package com.pospi.pai.yunpos.report;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lany.sp.SPHelper;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.pospi.callbacklistener.HttpCallBackListener;
import com.pospi.http.HttpConnection;
import com.pospi.modle.GoodsBeen;
import com.pospi.pai.yunpos.R;
import com.pospi.pai.yunpos.base.Base2Activity;
import com.pospi.pai.yunpos.been.BillDetailAdapter;
import com.pospi.pai.yunpos.been.ExpandLayout;
import com.pospi.pai.yunpos.been.SaveOrderInfo;
import com.pospi.pai.yunpos.login.Constant;
import com.pospi.util.DoubleSave;
import com.pospi.util.GetData;
import com.pospi.view.swipemenulistview.SwipeMenuListView;


import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class KHDetailActivity extends Base2Activity {
    @Bind(R.id.time_select)
    TextView time_select;
    @Bind(R.id.billno_xz)
    TextView billno_xz;
    @Bind(R.id.cgmd_xz)
    TextView cgmd_xz;
    @Bind(R.id.gys_xz)
    TextView gys_xz;
    @Bind(R.id.lv_goods_cgsh)
    SwipeMenuListView lv_goods_counts;
    @Bind(R.id.tv_money_xz)
    TextView tv_money_xz;
    @Bind(R.id.expandLayout)
    ExpandLayout expandLayout;
    @Bind(R.id.sh_bg)
    ImageView sh_bg;
    @Bind(R.id.tv_expand)
    TextView tv_expand;

    private List<GoodsBeen> goodsDtos = new ArrayList<>();

    private BillDetailAdapter adapter;
    private String id;
    private String type;
    private BillheadBeen billheadBeen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_kh_detail);
        ButterKnife.bind(this);
        setTitle("详情");
        id = getIntent().getStringExtra("id");
        type = getIntent().getStringExtra("type");

        expandLayout.initExpand(true);
        time_select.setText(GetData.getYYMMDDTime());
        adapter = new BillDetailAdapter(this, goodsDtos);
        lv_goods_counts.setAdapter(adapter);

        getData();

    }








    @OnClick({R.id.tv_expand})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_expand:
                expandLayout.toggleExpand();
                if (expandLayout.isExpand()) {
                    tv_expand.setBackground(getResources().getDrawable(R.drawable.drop_down_selected_icon));

                } else {
                    tv_expand.setBackground(getResources().getDrawable(R.drawable.drop_down_unselected_icon));
                }
                break;

        }
    }


    private void setJe() {
        double mo = 0;
        for (GoodsBeen dto : goodsDtos) {
            mo += dto.getSl() * dto.getDj();
        }
        tv_money_xz.setText(DoubleSave.doubleSaveTwo(mo) + "");
    }

    private void getData() {
        Map<String,String> params = new HashMap<>();
        params.put("model", "bill.mbillpf");
        params.put("fun", "find_detail");
        params.put("type", type);
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        JSONObject object = new JSONObject();
        try {
            object.put("id", id);
        } catch (JSONException e) {
        }

        params.put("pds", object.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("data", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONObject result = jsonObject.getJSONObject("result");
                    saveGoods(result);


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void saveGoods(JSONObject response) {
        List<GoodsBeen> list = new ArrayList<>();
        try {
            JSONArray array = response.getJSONArray("detail");
            JSONObject object = response.getJSONObject("head");
            billheadBeen = new Gson().fromJson(object.toString(), BillheadBeen.class);
            list = SaveOrderInfo.changegoodsToList(array.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        goodsDtos.addAll(list);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                time_select.setText(billheadBeen.getBillrq());
                billno_xz.setText(billheadBeen.getBillno());
                cgmd_xz.setText(billheadBeen.getMarketname());
                gys_xz.setText(billheadBeen.getCustname());
                adapter.notifyDataSetChanged();
                if (billheadBeen.getStatus().equals("2")) {
                    sh_bg.setVisibility(View.VISIBLE);
                }
                setJe();
            }
        });





    }



}
